<?php
error_reporting(0);
session_start();
include_once '../comm/config.php';
include_once '../comm/checkpostandget.php';
include_once 'check.php';
include_once 'page.class.php';
include_once '../comm/wjt.php';

if($_POST['do']=='del'){
    $ids=implode(',',$_POST['ids']);
	$sql="delete from ".$BIAOTOU."msg where Id IN(".$ids.")";
	mysql_query($sql);
	echo "<script>window.location='msg_list.php?act=".$_POST['act']."'</script>";
	exit;
}

$recuser=$_GET['senduser'];
//$outuser=$_POST['ddusername']; 
$content1=$_POST['content']; 
$act=$_GET['act'];
$page = !($_GET['page'])?'1':intval($_GET['page']);
$pagesize=10;
$page2=($page-1)*$pagesize;

if (!$act and !$outuser) {
	echo "缺少必要参数";
	exit;
}
else
{
	if($act=='in'){
		$sql="select * from ".$BIAOTOU."msg where ddusername='".$_SESSION["duoduouser"]."'";
		$rs = mysql_query($sql);
		$total = mysql_num_rows($rs);	
		
		$sql="select * from ".$BIAOTOU."msg where ddusername='".$_SESSION["duoduouser"]."' order by id desc limit $page2,$pagesize";
		$rs = mysql_query($sql);
	}
	if($act=='out'){
		$sql="select * from ".$BIAOTOU."msg where senduser='".$_SESSION["duoduouser"]."'";
		$rs = mysql_query($sql);
		$total = mysql_num_rows($rs);	
		
		$sql="select * from ".$BIAOTOU."msg where senduser='".$_SESSION["duoduouser"]."' order by id desc limit $page2,$pagesize";
		$rs = mysql_query($sql);
	}
	//发送短信
	if($act=='send'&&$content1!=''){
		if($_SESSION["duoduouser"]!='网站客服')
		{
			$sj= date("Y-m-d H:i:s");
			$q="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('站内消息','".$content1."','".$sj."',0,'网站客服','".$_SESSION["duoduouser"]."')";
			$rs = mysql_query($q);
		}
		echo "<script language=javascript>window.location='msg_list.php?act=out';</script>";
	}
}

$tag="center";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>短消息 - 用户中心</title>
<link href="../css/usercss.css" rel="stylesheet" type="text/css">
<link href="../template/<?=MOBAN?>/css/common.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jquery.js"></script>
<script language="javascript" type="text/javascript">
function getLength(str) {
    var len = str.length;
    var reLen = 0;
    for (var i = 0; i < len; i++) {        
        if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) {
            // 全角    
            reLen += 2;
        } else {
            reLen++;
        }
    }
    return reLen;    
}
function checkoutuser(outuser)
{
    	if(getLength(outuser)==0)
		{
			document.getElementById("ckddusername").innerHTML = '<img src=../images/cuo.gif alt="错误" align=absmiddle /> <font color=#ff0000>请输入消息接收人</font>';
		}
		else
		{
			document.getElementById("ckddusername").innerHTML = '<img src=../images/dui.gif alt="正确" align=absmiddle /> <font color=#009900></font>';
		}
}
function checkcontent(content)
{
    	if(getLength(content)==0)
		{
			document.getElementById("ckcontent").innerHTML = '<img src=../images/cuo.gif alt="错误" align=absmiddle /> <font color=#ff0000>请输入短信内容</font>';
		}
		else
		{
			document.getElementById("ckcontent").innerHTML = '<img src=../images/dui.gif alt="正确" align=absmiddle /> <font color=#009900></font>';
		}
}
function checkall()
{
	if(document.getElementById("ddusername").value==""||document.getElementById("content").value=="")
	{
		alert('请填写完内容！');
		return false;
	}
	else
	{
		return true;
	}
}

function selAll(obj){
    $(obj).attr("checked",'true');//全选
}
function selNone(obj){
    $(obj).removeAttr("checked");//取消全选
}
function selfan(obj){
    $(obj).each(function(){
		if($(this).attr("checked")){
			$(this).removeAttr("checked");
		}
		else{
		    $(this).attr("checked",'true');
		}
	})
}
</script>
</head>

<body id="msglist">
<?php
include("../template/".MOBAN."/header.tpl.php");
?>
<?php
include("../template/".MOBAN."/nav.tpl.php");
?>
<div id="main">
<?php
include("left.php");
?>
<div class="contain">
  <h2 class="left_nav4">我的站内消息</h2>
    <div class="order">
      <div class="title">
          <ul>
            <li><a style="MARGIN-LEFT: 10px" <?php if($act=='in') echo "class=\"current\"";?>
  href="msg_list.php?act=in">收到的消息</a></li>
  			<li><a style="MARGIN-LEFT: 10px" <?php if($act=='out') echo "class=\"current\"";?>
  href="msg_list.php?act=out">发出的消息</a></li>
  <li><a style="MARGIN-LEFT: 10px" <?php if($act=='send') echo "class=\"current\"";?>
  href="msg_list.php?act=send">联系客服</a></li>
          </ul>
      </div>    <div class="content">
      <ul style="width:710px">
      
	  	<?php if($act=='in'){?>
        <form method="post">
      <input type="hidden" name="do" value="del" />
      <input type="hidden" name="act" value="in" />
				<li class="order_title">
				<div class="item" style="width:30px"></div>
                <div class="item" style="width:40px;text-align:center" id="xuan">选中</div>
				<div class="item" style="width:100px; text-align:center">发送人</div>
				<div class="item" style="width:310px">消息内容</div>							
				<div class="item" style="width:130px;text-align:center;">发送时间</div>
				<div class="item" style="width:100px;text-align:center">执 行 操 作</div>
				</li>
			<?php
			while ($row = mysql_fetch_array($rs))
			{
				if($row["see"]=="1")
				{ $msgsrc="<div style=\" margin-top:7px; margin-left:5px;\"><img src=\"../images/msg-read.gif\" border=\"0\" align=\"absmiddle\"  alt=\"短消息\" /></div>";}
				else
				{$msgsrc="<div style=\" margin-top:7px; margin-left:5px;\"><img src=\"../images/msg.gif\" border=\"0\" align=\"absmiddle\" alt=\"新的短消息\" /></div>";}
				
				echo "<li><div class=\"item\" style=\"width:30px\">".$msgsrc."</div>";
				echo "<div class='item' style='width:40px;text-align:center'><input type='checkbox' class='checkbox' name='ids[]' value='".$row["Id"]."' /></div>";
				echo "<div class=\"item\" style=\"width:100px; text-align:center\">".$row["senduser"]."</div>";
				echo "<div class=\"item\" style=\"width:310px\"><a href=msg_read.php?msg_id=".$row["Id"].">".$row["content"]."</a></div>";
				echo "<div class=\"item\" style=\"width:130px;text-align:center\">".$row["addtime"]."</div>";					
				echo "<div class=\"item\" style=\"width:100px;text-align:center\"><a href=msg_read.php?msg_id=".$row["Id"].">阅读</a> <a href=msg_list.php?act=send&senduser=".urlencode($row["senduser"]).">回复</a> <a  onclick=\"return confirm('您确定要删除吗?')\" href=msg_del.php?id=".$row["Id"].">删除</a></div>";
				echo "</li>";
			}
			?>
            <li><div class="item" style=""><a href="javascript:selAll('.checkbox')">全选</a> <a href="javascript:selNone('.checkbox')">取消全选</a> <a href="javascript:selfan('.checkbox')">反选</a> <input type="submit" value="删除" onclick='return confirm("确定要删除?")' style="background:none; border:0px" /></div></li>
                </form>
            </ul>
			<?php }?>	
			
	  		<?php if($act=='out'){?>
            <form method="post">
      <input type="hidden" name="do" value="del" />
      <input type="hidden" name="act" value="out" />
				<li class="order_title">
				<div class="item" style="width:30px"></div>
                <div class="item" style="width:40px;text-align:center" id="xuan">选中</div>
				<div class="item" style="width:100px; text-align:center">接收人</div>
				<div class="item" style="width:420px">消息内容</div>							
				<div class="item" style="width:120px;text-align:center;">发送时间</div>
				</li>
			<?php
			while ($row = mysql_fetch_array($rs))
			{
				if($row["see"]=="1")
				{ $msgsrc="<div style=\" margin-top:7px;margin-left:5px;\"><img src=\"../images/msg-read.gif\" border=\"0\" align=\"absmiddle\"  alt=\"短消息\" /></div>";}
				else
				{$msgsrc="<div style=\" margin-top:7px;margin-left:5px;\"><img src=\"../images/msg.gif\" border=\"0\" align=\"absmiddle\" alt=\"新的短消息\" /></div>";}
				echo "<li><div class=\"item\" style=\"width:30px\">".$msgsrc."</div>";
				echo "<div class='item' style='width:40px;text-align:center'><input type='checkbox' class='checkbox' name='ids[]' value='".$row["Id"]."' /></div>";
				echo "<div class=\"item\" style=\"width:100px;text-align:center\">".$row["ddusername"]."</div>";
				echo "<div class=\"item\" style=\"width:420px\">".$row["content"]."</div>";
				echo "<div class=\"item\" style=\"width:120px;text-align:center\">".$row["addtime"]."</div>";
				echo "</li>";
			}
			?>
            <li><div class="item" style=""><a href="javascript:selAll('.checkbox')">全选</a> <a href="javascript:selNone('.checkbox')">取消全选</a> <a href="javascript:selfan('.checkbox')">反选</a> <input type="submit" value="删除" onclick='return confirm("确定要删除?")' style="background:none; border:0px" /></div></li>
                </form>
            </ul>
			<?php }?>
			<?php
			if($act=='send'){
		  ?>
	  	  <FORM onsubmit="return checkall()" method=post action="msg_list.php?act=send">
		  <LI style="LINE-HEIGHT: 40px; HEIGHT: 40px; CLEAR: both;width:600px; border-bottom:0px;"><LABEL>收件人：</LABEL><INPUT id="ddusername" size="60" type=text name="ddusername" style="width:300px;BORDER-BOTTOM: #c9c9c9 1px solid; BORDER-LEFT: #c9c9c9 1px solid; LINE-HEIGHT: 25px; HEIGHT: 25px; FONT-SIZE: 12px; BORDER-TOP: #c9c9c9 1px solid; BORDER-RIGHT: #c9c9c9 1px solid" value="网站客服" readonly="true" disabled="disabled">
		  </LI>
		  <LI style="LINE-HEIGHT: 40px; HEIGHT: 170px; CLEAR: both; width:700px; border-bottom:0px;"><LABEL>内　容：</LABEL><TEXTAREA style=" HEIGHT: 160px; width:500px; BORDER-BOTTOM: #c9c9c9 1px solid; BORDER-LEFT: #c9c9c9 1px solid; LINE-HEIGHT: 26px; FONT-SIZE: 12px; BORDER-TOP: #c9c9c9 1px solid; BORDER-RIGHT: #c9c9c9 1px solid" rows=10 cols=50 name="content" id="content" onblur="checkcontent(document.getElementById('content').value);"></TEXTAREA> 
		  <LABEL id="ckcontent" style="WIDTH: 100px; COLOR: #f00">*</LABEL></LI>
		  <LI style="LINE-HEIGHT: 40px; HEIGHT: 40px; CLEAR: both; border-bottom:0px; width:300px"><LABEL></LABEL><INPUT style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; FONT-SIZE: 12px; BORDER-TOP: 0px; BORDER-RIGHT: 0px; margin-left:50px;" class=smtgray value=立即发送 type=submit> 
		  </LI></FORM>
		  <?php
		  }
		  ?>	
	</div>
	</div>
</div>
	<div class="megas512" style="clear:both"><?php pageft($total,$pagesize,1,1,0,10,"msg_list.php?act=".$act."");	echo $pagenav;?></div>
</div>
<?php
include("../template/".MOBAN."/footer.tpl.php");
mysql_close();
?>
</body>
</html>